Retain submitted input in acceptPost exercises#1477
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enables retention of submitted input values in acceptPost exercises, allowing students to easily modify their previous submissions. The implementation detects acceptPost exercises by checking for textareas without fieldsets and fills them with the last submission data on page load.
- Added detection logic to identify acceptPost exercises based on textarea and fieldset presence
- Extended the
loadLastSubmissionfunction to support filling input fields with previous submission data - Updated the CSS selector for last submission links to use the correct navbar class
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if (exercise.active_element) { | ||
| exercise.loadLastSubmission($(data)); | ||
| } else if (hasTextarea && !hasFieldset) { // Identify acceptPost exercises |
There was a problem hiding this comment.
The logic to identify acceptPost exercises using hasTextarea && !hasFieldset is fragile and could incorrectly classify other exercise types. Consider using a more explicit identifier such as a data attribute or exercise type property to reliably distinguish acceptPost exercises.
| if (exercise.active_element) { | |
| exercise.loadLastSubmission($(data)); | |
| } else if (hasTextarea && !hasFieldset) { // Identify acceptPost exercises | |
| const exerciseType = exercise.element.data('exercise-type'); | |
| if (exercise.active_element) { | |
| exercise.loadLastSubmission($(data)); | |
| } else if (exerciseType === 'acceptPost') { // Identify acceptPost exercises explicitly |
| textareas.each(function(index) { | ||
| $(this).val(lastInputs[index]); | ||
| }); |
There was a problem hiding this comment.
No bounds checking when mapping submission data to textareas. If there are more textareas than submission data entries, or vice versa, this could result in undefined values or missed textareas. Add validation to ensure the arrays have matching lengths.
| textareas.each(function(index) { | |
| $(this).val(lastInputs[index]); | |
| }); | |
| const minLen = Math.min(textareas.length, lastInputs.length); | |
| // Set values for matching indices | |
| textareas.each(function(index) { | |
| if (index < lastInputs.length) { | |
| $(this).val(lastInputs[index]); | |
| } else { | |
| // Clear extra textareas if there are more textareas than inputs | |
| $(this).val(''); | |
| } | |
| }); | |
| if (textareas.length !== lastInputs.length) { | |
| // Optionally log a warning for debugging | |
| if (window.console && console.warn) { | |
| console.warn( | |
| "Mismatch between number of textareas (" + | |
| textareas.length + | |
| ") and submission data entries (" + | |
| lastInputs.length + | |
| ")." | |
| ); | |
| } | |
| } |
da67ed2 to
f0e54a2
Compare
ihalaij1
left a comment
There was a problem hiding this comment.
Great!
I tested this and it works well.
Description
What?
Retain the submitted input value in the input field after submission in exercises defined with
view_type: access.types.stdasync.acceptPost.Why?
Students want to easily tweak their submission.
How?
When the exercise is reloaded, insert the last submitted input into the input field.
Fixes #1475
Testing
Remember to add or update unit tests for new features and changes.
What type of test did you run?
I tested that the last submission persists in the input field after submission and after page refresh. This was tested also with exercises that have multiple input fields in the O1 course.
Did you test the changes in
Think of what is affected by these changes and could become broken
Translation
Programming style
Have you updated the README or other relevant documentation?
Is it Done?
Clean up your git commit history before submitting the pull request!